import { ref } from 'vue';

import { deserialize } from '@/plugins/reflect-metadata';
import type { ListState } from '@/types';

import { getCardOrderDetail } from '../api';
import { CardOrderDetailItem } from '../models/CardOrderItem';
import { CardOrderDetailList, CardOrderQuery } from '../models/CardOrderList';

const useCardOrderActions = () => {
  const params = ref<CardOrderQuery>(new CardOrderQuery());
  const pageState = ref({
    finished: false,
    refreshing: false,
    error: false,
  });

  const state = ref<ListState<CardOrderDetailItem>>({
    data: [],
    loading: false,
    total: 0,
  });

  const fetchData = async (id: string) => {
    try {
      const { data } = await getCardOrderDetail(id);
      const resultData = deserialize(data, CardOrderDetailList);
      state.value.data = resultData.items;
      state.value.total = resultData.total;
    } catch {
      //
    } finally {
      state.value.loading = false;
    }
  };

  return {
    params,
    pageState,
    state,
    fetchData,
  };
};

export default useCardOrderActions;
